package com.dongdongshop.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdongshop.data.HttpResponseEnum;
import com.dongdongshop.data.Result;
import com.dongdongshop.pojo.address.TbAddress;
import com.dongdongshop.pojo.address.TbAreas;
import com.dongdongshop.pojo.address.TbCities;
import com.dongdongshop.pojo.address.TbProvinces;
import com.dongdongshop.pojo.user.TbUser;
import com.dongdongshop.service.AddressServiceI;
import com.dongdongshop.service.AreasServiceI;
import com.dongdongshop.service.CitieServiceI;
import com.dongdongshop.service.ProvincesServiceI;
import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("address")
public class AddressController {

    private final Logger logger= LoggerFactory.getLogger(AddressController.class);

    @Reference
    private AddressServiceI addressServiceI;
    @Reference
    private ProvincesServiceI provincesServiceI;
    @Reference
    private CitieServiceI citieService;
    @Reference
    private AreasServiceI areasServiceI;

    @RequestMapping("showAddress")
    public String showAddress(Model model){
        TbUser user =(TbUser) SecurityUtils.getSubject().getPrincipal();
        model.addAttribute("user",user);
        return "home-setting-address";
    }

    @RequestMapping("listAddress")
    @ResponseBody
    public Result<List<TbAddress>> listAddress(){
       try {
           //根据用户名插叙你地址
           TbUser user =(TbUser) SecurityUtils.getSubject().getPrincipal();
     //      System.out.println("登录名"+user.getUsername());
          List<TbAddress> addressList = addressServiceI.listAddress(user.getUsername());
          return Result.jsonResponse(HttpResponseEnum.OK).setData(addressList);
       }catch (Exception e){
           logger.error("系统错误",e);
           return Result.jsonResponse(HttpResponseEnum.FAIL);
       }
    }

    //省
    @RequestMapping("getProvinces")
    @ResponseBody
    public Result<List<TbProvinces>> getProvinces(){
        try {
            List<TbProvinces> provincesList=provincesServiceI.getProvinces();
            return Result.jsonResponse(HttpResponseEnum.OK).setData(provincesList);
        }catch (Exception e){
            logger.error("系统错误",e);
            return Result.jsonResponse(HttpResponseEnum.FAIL);
        }
    }
    //市
    @RequestMapping("getCity")
    @ResponseBody
    public Result<List<TbCities>> getCity(String provinceid){
        try {
            List<TbCities> provincesList=citieService.getCity(provinceid);
            return Result.jsonResponse(HttpResponseEnum.OK).setData(provincesList);
        }catch (Exception e){
            logger.error("系统错误",e);
            return Result.jsonResponse(HttpResponseEnum.FAIL);
        }
    }
    //县
    @RequestMapping("getCounty")
    @ResponseBody
    public Result<List<TbAreas>> getCounty(String cityid){
        try {
            List<TbAreas> provincesList=areasServiceI.getCounty(cityid);
            return Result.jsonResponse(HttpResponseEnum.OK).setData(provincesList);
        }catch (Exception e){
            logger.error("系统错误",e);
            return Result.jsonResponse(HttpResponseEnum.FAIL);
        }
    }
    //删除
    @RequestMapping("deleteAddress/{id}")
    @ResponseBody
    public Result deleteAddress(@PathVariable("id")Long id){
        try {
            addressServiceI.deleteAddress(id);
            return Result.jsonResponse(HttpResponseEnum.OK);
        }catch (Exception e){
            logger.error("系统错误",e);
            return Result.jsonResponse(HttpResponseEnum.FAIL);
        }
    }

    //增加
    @RequestMapping("insertAddress")
    @ResponseBody
    public Result insertAddress(TbAddress address){
        try{
            TbUser user = (TbUser) SecurityUtils.getSubject().getPrincipal();
            address.setUserId(user.getUsername());
            addressServiceI.insertAddress(address);
            return Result.jsonResponse(HttpResponseEnum.OK);
        }catch (Exception e){
            logger.error("系统错误",e);
            return Result.jsonResponse(HttpResponseEnum.FAIL);
        }
    }
    //修改默认地址
    @RequestMapping("setDefault/{id}")
    @ResponseBody
    public Result setDefault(@PathVariable("id")Long id){
        try {
            TbUser user = (TbUser) SecurityUtils.getSubject().getPrincipal();
            addressServiceI.isDefault(user.getUsername(),id);
            return Result.jsonResponse(HttpResponseEnum.OK);
        }catch (Exception e){
            logger.error("系统错误",e);
            return Result.jsonResponse(HttpResponseEnum.FAIL);
        }
    }
}
